package echos

import (
  "fmt";"strconv"
  "sync"
  "jinquan.supor.com/common"
)

var (
  mu sync.Mutex
  TotalNumOfUser int64
)

func connCache() *common.RdsCcWorker {
    conn := common.CacheConn{
    RedisIp: "172.24.16.16",
    RedisPort: "6379",
    Requirepass: "4aKadedr",
  }
  ccw := common.Redis1nd_CacheHandle(conn)
  return ccw
}

func APP_Redis_Init() {
  ccw := connCache()
  ret := ccw.Redis1nd_IsHashFieldExists("MSPSVC", "TotalNumOfUser")
  if ret == false {
    ccw.Redis1nd_HashValStore("MSPSVC", "TotalNumOfUser", "0")
    TotalNumOfUser = 0
    fmt.Printf("Redis/> Global Parameter h[MSPSVC]:f[TotalNumOfUser]:v[%d] created.\n", TotalNumOfUser)
  }else{
    TotalNumOfUser, _ := strconv.ParseInt(ccw.Redis1nd_HashValEject("MSPSVC", "TotalNumOfUser"), 0, 64)
    fmt.Printf("Redis/> Global Parameter h[MSPSVC]:f[TotalNumOfUser]:v[%d] fetched.\n", TotalNumOfUser)
  }
}

func APP_TotalNumOfUser_Get(ccw *common.RdsCcWorker) int64 {
  mu.Lock()
  n := TotalNumOfUser
  mu.Unlock()
  return n
}

func APP_TotalNumOfUser_Inc(ccw *common.RdsCcWorker) {
  mu.Lock()
  TotalNumOfUser += 1
  ccw.Redis1nd_HashValStore("MSPSVC", "TotalNumOfUser", strconv.Itoa((int)(TotalNumOfUser)) )
  mu.Unlock()
}

type userinfoHashStruct struct{
  UserId string
  Username string
  Password string
  Email  string
  Hiredate string
  Regtime string
}

func APP_registHashValStore(ccw *common.RdsCcWorker, u userinfoHashStruct, primaryKey string) {
    ccw.Redis1nd_HashValStore(primaryKey, "UserId", u.UserId)
    ccw.Redis1nd_HashValStore(primaryKey, "Username", u.Username)
    ccw.Redis1nd_HashValStore(primaryKey, "Password", u.Password)
    ccw.Redis1nd_HashValStore(primaryKey, "Email", u.Email)
    ccw.Redis1nd_HashValStore(primaryKey, "Hiredate", u.Hiredate)
    ccw.Redis1nd_HashValStore(primaryKey, "Regtime", u.Regtime)
}
